import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {
    Row,
    Col,
    Card,
    Form,
    Input,
    Select,
    Button,
    Modal,
    Tabs,
    Radio,
    message
} from 'antd';

import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import ShippingTempletAttr from '../../components/shipping/sellerEdit/index';
import {powerControl} from '../../utils/powerControl';
const AuthButton = powerControl(Button);
import FreightTemplateEdit from '../../components/shipping/sellerEdit/FreightTemplateEdit';
const TabPane = Tabs.TabPane;
const FormItem = Form.Item;
const {Option} = Select;
const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
const RadioGroup = Radio.Group;
@connect(state => ({
    rule: state.shipping
}))
@Form.create()
export default class ShippingSellerDetails extends PureComponent {
    state = {
        modalVisible: false,
        editType: 'add',
        formValues: {},
        record: {},
        records:{},
        selectedRows: [],
        dataShipping:[],
        shipping_templet_id:'',
    }


    componentDidMount(){
        const {dispatch} = this.props;
        const record  = this.props.location.state.record;
        this.setState({
            shipping_templet_id:record.shipping_templet_id,
        })
       const params = {
           shipping_templet_id:record.shipping_templet_id,
       }
        this.setState ({
            record:record ,
        })
        this.props.dispatch({
            type: 'shipping/queryFreightTemplate',
            payload:params
        });
        this.props.dispatch({
            type: 'shipping/queryShipping',
        });

    }
    handleSelectRows = (rows) => {

        this.setState({
            selectedRows: rows,
        });
    }


    handleEditTypeRow = (record, pagination) => {
        this.handleModalVisible(true);
        this.setState({
            editType: 'mod',
            records:record
        });
    }
    success=()=>{
        const params = {
            shipping_templet_id:this.state.shipping_templet_id,
        }
        this.props.dispatch({
            type: 'shipping/queryFreightTemplate',
            payload:params
        });
    }

    handleAddType = (value) => {

        value={
           shipping_name:value,
            shipping_templet_id:this.state.shipping_templet_id,
            shipping_area_ids :[]
        }
        this.handleModalVisible(true);
        this.setState({
            editType: 'add',
            formValues:value,
        });
    }
    handleModalVisible = (flag) => {
        this.setState({
            modalVisible: !!flag,
        });
    }
    handleDelTypeRow = (shipping_templet_attr_id, pagination) => {
        const {dispatch} = this.props;
        const {record}  = this.state;
        dispatch({
            type: 'shipping/delShippingTempletAttr',
            payload: {shipping_templet_attr_id: shipping_templet_attr_id},
            callback: (response) => {
                if (response.result === 0) {
                    message.success('删除成功');
                    const params = {
                        shipping_templet_id:this.state.shipping_templet_id,
                    }
                    this.props.dispatch({
                        type: 'shipping/queryFreightTemplate',
                        payload:params
                    });
                } else {
                    message.success('删除失败');
                }

            }
        });
    }

    handleSubmit = (e) => {
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                this.props.dispatch({
                    type: 'shipping/updateShippingTemplet',
                    payload: {...values},
                    callback: (response) => {
                        if (response.result === 0) {
                            message.success('提交成功');

                        } else {
                            message.success('提交失败');
                        }
                    }
                });
            }
        });
    }




    render(){

        const {modalVisible,editType,record,selectedRows} = this.state;
        const {rule: {loading: ruleLoading, data,typesSList,typeList,dataShipping}} = this.props;
        const {getFieldDecorator} = this.props.form;
        const formInitValue = record;
        this.state.dataShipping = dataShipping;


        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 15},
            },
        };

        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };




        return(


            <PageHeaderLayout title="模板详情">
                <Card bordered={false}>
                    <Form onSubmit={this.handleSubmit}>
                        <FormItem
                            {...formItemLayout}
                            label=""
                        >
                            {(getFieldDecorator("shipping_templet_id", {initialValue:formInitValue.shipping_templet_id}))(
                                <Input hidden/>
                            )}
                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="模板名称"
                        >
                            {(getFieldDecorator("shipping_templet_name", {initialValue:formInitValue.shipping_templet_name}))(
                                <Input />
                            )}

                        </FormItem>

                        <FormItem
                            {...formItemLayout}
                            label="运费标题"
                        >
                            {(getFieldDecorator("shipping_title", {initialValue:formInitValue.shipping_title}))(
                               <Input />
                            )}

                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="配送方式"
                        >
                            {(getFieldDecorator("shipping_id", {initialValue:''}))(
                                <Select placeholder="请选择"  style={{width: '100%'}} onSelect={this.handleAddType}>
                                    <Option value="">请选择</Option>
                                    {

                                        this.state.dataShipping.map(function (t) {
                                            return (
                                                <Option value={t.shipping_name}>{t.shipping_name}</Option>
                                            )})
                                    }
                                </Select>
                            )}

                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="运费模板"
                        >

                            <ShippingTempletAttr
                                 selectedRows={selectedRows}
                                 loading={ruleLoading}
                                    data={data}
                                 onSelectRow={this.handleSelectRows}
                                 onChange={this.handleSellerListChange}
                                 onDelRow={this.handleDelTypeRow}
                                 onEditRow={this.handleEditTypeRow}
                            />
                        </FormItem>
                        <FormItem {...submitFormLayout} style={{marginTop: 32}}>
                            <Button type="primary"  onClick={this.handleSubmit.bind(this)} >
                                确定
                            </Button>
                        </FormItem>
                    </Form>

                </Card>

                <Modal
                    title="编辑运费模板"
                    key={new Date()}
                    visible={modalVisible}
                    onOk={this.handleAdd}
                    width={800}
                    onCancel={() => this.handleModalVisible()}
                    footer={null}

                >
                    <FreightTemplateEdit  ref="FreightTemplateEdit"
                                   pagination={data.pagination}
                                   editType={editType}
                                   records={this.state.records}
                                          formValues={this.state.formValues}
                                   handleModalVisible={this.handleModalVisible}
                                          success={this.success}
                                   FreightTemplateSList={typesSList}
                    />

                </Modal>

            </PageHeaderLayout>
                )

    }





}